<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%nav_menu}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $controllers
 * @property string $action
 * @property string $param
 * @property integer $status
 * @property integer $open_mode
 * @property integer $type_id
 * @property integer $admin_id
 * @property integer $position
 * @property integer $created_at
 */
class NavMenu extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%nav_menu}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'open_mode', 'type_id', 'admin_id', 'position', 'created_at','sort_order'], 'integer'],
            [['name', 'controllers', 'action', 'param','type'], 'string', 'max' => 100],
            [['name', 'type_id', 'position','controllers','action'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'name' => Yii::t('zhtfrontend', '菜单名称'),
            'controllers' => Yii::t('zhtfrontend', '控制器'),
            'action' => Yii::t('zhtfrontend', '方法'),
            'param' => Yii::t('zhtfrontend', '参数'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'open_mode' => Yii::t('zhtfrontend', '0 直接跳转 1 新页面打开'),
            'type_id' => Yii::t('zhtfrontend', '三大类'),
            'admin_id' => Yii::t('zhtfrontend', '管理员id'),
            'position' => Yii::t('zhtfrontend', '位置'),
            'sort_order' => Yii::t('zhtfrontend', '排序'),
            'type' => Yii::t('zhtfrontend', '模块'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['position'] = function () {
            switch ($this->position){
                case 1:
                    return \Yii::t('zhtfrontend','顶部');
                case 2:
                    return \Yii::t('zhtfrontend','左侧');
                case 3:
                    return \Yii::t('zhtfrontend',' 底部');
            }
        };
        $fields['type_id'] = function (){
            switch ($this->type_id){
                case 1:
                    return \Yii::t('zhtfrontend','益智');
                case 2:
                    return \Yii::t('zhtfrontend','音乐');
                case 3:
                    return \Yii::t('zhtfrontend',' 形体');
            }
        };
        $fields['status']= function (){
            switch ($this->status){
                case 1:
                    return \Yii::t('zhtfrontend','启用');
                case 2:
                    return \Yii::t('zhtfrontend','禁用');
            }
        };
        $fields['type']= function (){
            switch ($this->type){
                case 'news':
                    return \Yii::t('zhtfrontend','新闻');
                case 'teacher':
                    return \Yii::t('zhtfrontend','老师');
                case 'goods':
                    return \Yii::t('zhtfrontend','课程/商品');
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
